<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品出库</title>
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.css"/>
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
</head>
<body>
<div id="vuebox">
    <div class="container">
        <div style="text-align: center">
            <h1>商品出库</h1>
            出库商品:<select v-model="new_entey.id">
            <option v-for="item in products" :value="item.id">{{item.productname}}</option>
        </select><br><br>
            数量:<input v-model="new_entey.quantity" type="text"><br><span id="msg" style="color: red"></span><br><br>
            经手人:<input type="text" v-model="new_entey.handler"><br>
            <button @click="sub()">提交</button>
            <button @click="reset()">重置</button>
        </div>
    </div>
</div>

</body>
</html>
<script type="text/javascript">
    var vm=new Vue({
        el:"#vuebox",
        data:{
            products:{},
            new_entey:{
                id:"1",
                handler:"",
                quantity:""
            },
            record:{
                productid:"",
                quantity:"",
                handler:"",
            }
        },
        methods:{
            reset:function () {
                this.new_entey.handler ="";
                this.new_entey.quantity="";
                this.new_entey.id="1";
                $('#msg').html("");
            },
            sub:function () {
                if (this.new_entey.quantity==""||this.new_entey.handler==""){
                    alert("请不要提交空数据")
                }else{
                    axios.get("http://localhost:8081/selectProductById",{params:{id:this.new_entey.id}}).then(res=>{
                        if (res.data.quantity>=this.new_entey.quantity){
                            axios.post("http://localhost:8081/updateProduct",this.new_entey).then(res=>{
                                if (res.data.result>0){
                                    this.record.productid=this.new_entey.id;
                                    this.record.quantity=this.new_entey.quantity;
                                    this.record.handler=this.new_entey.handler;
                                    axios.post("http://localhost:8081/addTakeout",this.record).then(res=>{
                                        if (res.data>0){
                                            alert("出库成功")
                                            $('#msg').html("")
                                        }else{
                                            alert("出库成功但记录失败,请联系管理员")
                                            $('#msg').html("")
                                        }
                                    })

                                }else{
                                    alert("出库失败,请联系管理员")
                                    $('#msg').html("")

                                }
                            })
                        }else{
                            $('#msg').text("库存不足,当前库存数量为:"+res.data.quantity);
                        }
                    })
                }
            }

        },mounted(){
            axios.get("http://localhost:8081/selectAllProduct").then(res=>{
                this.products=res.data;
            })
        }

    })
</script>